package com.common.manager;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.common.model.MenuInfoModel;
import com.common.model.RoleMenuFuncModel;
import com.common.model.StaffInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;


@Component
public class MenuInfoManager  extends BaseManager{
	
	@Override
	public void setSqlMapNameSpace() {
		this.sqlMapNameSpace = "com.common.security.model.MenuInfo";
	}
		
	
	/**
	 * 功能：通过角色加载顶级机构菜单
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public List<MenuInfoModel>  getTopMenuByRoleId(String roleIds){
		IData idata = new DataMap();
		idata.put("roleIds", roleIds);
		idata.put("parentId", "0");
		return  super.getObjectList("getRoleMenuList",idata);
	}
	
	
	/**
	 * 功能：加载左侧子菜单树
	 * @param sysCode
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public String loadSystemMenuTree(StaffInfoModel staffInfoModel,String parentId) throws Exception {
		IData idata = new DataMap();
		idata.put("roleIds", staffInfoModel.getRoleids());
		idata.put("parentId", parentId);
		List<MenuInfoModel> menuist = super.getObjectList("getRoleMenuList",idata);
		JSONArray orgList = new JSONArray();
		JSONObject orgObj;
		if(menuist!=null){
			for (MenuInfoModel menuInfo : menuist) {
				 orgObj = new JSONObject();
				orgObj.put("id", menuInfo.getIntID());
				orgObj.put("pId", menuInfo.getParentId());
				orgObj.put("name", menuInfo.getMenuName());
				orgObj.put("open","true");
				orgObj.put("url",menuInfo.getMenuUrl());
				orgObj.put("tabid","tab_menu");
				orgObj.put("fresh","true");
				orgList.add(orgObj);
			}
		}
		return orgList.toString();
	}
	
	/**
	 * 功能：角色菜单授权树
	 * @param roleId
	 * @return
	 */
	public String getMenuFuncTreeList(String roleId){
		IData idata = new DataMap();
		idata.put("roleId", roleId);
		JSONArray treeList = new JSONArray();
		List<RoleMenuFuncModel> menuFuncList = super.getObjectList("getMenuFuncTreeList",idata);
		if(menuFuncList!=null){
			JSONObject orgObj = new JSONObject();
			for (int i = 0; i < menuFuncList.size(); i++) {
				//jsonString.append("{id:1, pId:0, name:\"父节点1 - 展开\", open:true},");
				RoleMenuFuncModel roleMenuFuncModel =  (RoleMenuFuncModel) menuFuncList.get(i);
				orgObj = new JSONObject();
				orgObj.put("id", roleMenuFuncModel.getId());
				orgObj.put("pId", roleMenuFuncModel.getPId());
				orgObj.put("name", roleMenuFuncModel.getLabel());
				if("0".equals(roleMenuFuncModel.getPId())){
					orgObj.put("open","true");
				}
				if(roleMenuFuncModel.getSelFlag()!=null){
					orgObj.put("checked", true);
				}
				if("F".equals(roleMenuFuncModel.getTypeFlag())){
					orgObj.put("icon", "static/ztree/3.png");
				}else{
					orgObj.put("icon", "static/ztree/8.png");
				}
				orgObj.put("typeFlag", roleMenuFuncModel.getTypeFlag());
				orgObj.put("intID", roleMenuFuncModel.getIntID());
				treeList.add(orgObj);
			}
		}
	  log.info(treeList.toString());
	  return treeList.toString();
	}
	
}
